package com.shop.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * ConstraintValidator<校验注解,校验类型>
 */
public class ShowStatusConstraintValidator implements ConstraintValidator<ShowStatus,Integer> {

    Set<Integer> set = new HashSet<>();

    //初始化方法
    @Override
    public void initialize(ShowStatus constraintAnnotation) {
        //拿到我们的值
        int[] value = constraintAnnotation.value();
        if (value != null){
            for (Integer val : set) {
                set.add(val);
            }

        }

    }


    /**
     *判断是否校验成功
     * @param value  校验的信息
     * @param context  上下文环境
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {

        if(set.contains(value)){
            return true;
        }

        return false;
    }
}
